﻿function getCSSRule(ruleName, deleteFlag)
{
	ruleName = ruleName.toLowerCase();
	if (document.styleSheets)
	{
		for (var i = 0; i < document.styleSheets.length; i++)
		{
			var styleSheet = document.styleSheets[i];
			var ii = 0;
			var cssRule = false;
			do
			{
				if (styleSheet.cssRules)
				{
					cssRule = styleSheet.cssRules[ii];
				} else
				{
					cssRule = styleSheet.rules[ii];
				}
				if (cssRule)
				{
					if (cssRule.selectorText.toLowerCase() == ruleName)
					{
						if (deleteFlag == 'delete')
						{
							if (styleSheet.cssRules)
							{
								styleSheet.deleteRule(ii);
							} else
							{
								styleSheet.removeRule(ii);
							}
							return true;
						} else
						{
							return cssRule;
						}
					}
				}
				ii++;
			} while (cssRule)
		}
	}
	return false;
}

function killCSSRule(ruleName)
{
	return getCSSRule(ruleName, 'delete');
}

function addCSSRule(ruleName)
{
	if (document.styleSheets)
	{
		if (!getCSSRule(ruleName))
		{
			if (document.styleSheets[0].addRule)
			{
				document.styleSheets[0].addRule(ruleName, null, 0);
			} else
			{
				document.styleSheets[0].insertRule(ruleName + ' { }', 0);
			}
		}
	}
	return getCSSRule(ruleName);
}





function GetNumValueFromStyle(style)
{
	if (style == null)
		return 0;
	var styleText = new Array("px", "%", "em");
	for (var i = 0; i < styleText.length; ++i)
	{
		if (style.indexOf(styleText[i]) != -1)
		{
			var v = parseInt(style.substr(0, style.indexOf(styleText[i])));
			if (isNaN(v))
				return 0;
			else
				return v;
		}
	}
	return 0;
}






if (typeof (Element) != null)
{
	Element.prototype.setSelectionEnabled = Element.prototype.set_SelectionEnabled = document.set_SelectionEnabled = function set_SelectionEnabled(value)
	{
		if (value)
		{
			if (typeof (this.onselectstart) != "undefined" && this._saveOnSelectStart != null)
				this.onselectstart = this._saveOnSelectStart;
			else if (this.style != null && typeof (this.style["MozUserSelect"]) != "undefined")
				this.style["MozUserSelect"] = "";
			else if (this._saveOnMouseDown != null)
				this.onmousedown = this._saveOnMouseDown;
		}
		else
		{
			if (typeof (this.onselectstart) != "undefined")
			{
				this._saveOnSelectStart = this.onselectstart;
				this.onselectstart = function onselectstart() { return false; };
			}
			else if (this.style != null && typeof (this.style["MozUserSelect"]) != "undefined")
				this.style["MozUserSelect"] = "none";
			else
			{
				this._saveOnMouseDown = this.onmousedown;
				this.onmousedown = function onmousedown() { return false; };
			}
		}
	}
	Element.prototype.get_ComputedStyle = function get_ComputedStyle()
	{
		if (window.getComputedStyle != null)
			return window.getComputedStyle(this, null);
		return this.currentStyle;
	}
	Element.prototype.set_Width = function set_Width(width)
	{
		width -= GetNumValueFromStyle(this.get_ComputedStyle().paddingRight)
			+ GetNumValueFromStyle(this.get_ComputedStyle().paddingLeft)
			+ GetNumValueFromStyle(this.get_ComputedStyle().borderRightWidth)
			+ GetNumValueFromStyle(this.get_ComputedStyle().borderLeftWidth);
		if (width < 0) width = 0;
		this.style["width"] = width + "px";
	}
	Element.prototype.set_Height = function set_Height(height)
	{
		height -= GetNumValueFromStyle(this.get_ComputedStyle().paddingTop)
			+ GetNumValueFromStyle(this.get_ComputedStyle().paddingBottom)
			+ GetNumValueFromStyle(this.get_ComputedStyle().borderTopWidth)
			+ GetNumValueFromStyle(this.get_ComputedStyle().borderBottomWidth);
		if (height < 0) height = 0;
		this.style["height"] = height + "px";
	}
	Element.prototype.set_Opacity = function set_Opacity(value)
	{
		this.style["filter"] = "alpha(opacity='" + value + "')";
		this.style["mozOpacity"] = value / 100;
		this.style["opacity"] = value / 100;
	}

}